//创建学校相关小仓库
import { defineStore } from "pinia";
import { reqSchoolAll, reqSchoolAdd, reqSchoolList, reqSchoolById, reqSchoolDelete, reqSchoolUpdate } from "@/api/school";
import type {
    schoolInfoData, schoolListResponseData
    , schoolResponseData, schoolAllResponseData, schoolListFormData
} from "@/api/school/type";
import type{ SchoolState } from "./types/types";
let useSchoolStore = defineStore('School', {
    state: (): SchoolState => {
        return {
            schools: []
        }
    },
    actions: {
        //获取所有学校
        async getSchoolAll() {
            let result:schoolAllResponseData=await reqSchoolAll();
            if(result.status == 0){
                this.schools=(result.data as schoolInfoData[])
                return  'ok'
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //分页获取学校
        async getSchoolList(data:schoolListFormData){
            let result :schoolListResponseData= await reqSchoolList(data);
            if(result.status == 0){
                this.schools=(result.data?.data as schoolInfoData[])
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //添加学校
        async addSchool(data:schoolInfoData){
            let result :schoolResponseData= await reqSchoolAdd(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //根据id查询学校
        async getSchoolById(_id:string){
            let result :schoolResponseData= await reqSchoolById(_id);
            if(result.status == 0){
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //更新学校信息
        async updateSchool(data:schoolInfoData){
            let result :schoolResponseData= await reqSchoolUpdate(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //删除学校
         async deleteSchool(schoolId:string){
            let result :schoolResponseData= await reqSchoolDelete(schoolId);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
    },
})
export default useSchoolStore
























